<?php

namespace App\Admin\Controllers;

use App\Models\HomeConfig;
use Encore\Admin\Form;
use Encore\Admin\Http\Controllers\AdminController;
use Encore\Admin\Layout\Content;
use Encore\Admin\Table;

class BannerConfigsController extends AdminController
{
    protected $title = '首页配置';

    public function index(Content $content)
    {
        return redirect('admin/home_configs/1/edit');
    }

    protected function table()
    {
        $table = new Table(new HomeConfig());

        $table->column('id', __('ID'));
        $table->column('type', __('类型'))->display(function ($value) {
            return HomeConfig::$typeMap[$value];
        });
        $table->column('mp4_or_image', __('图片或者视频'))->copyable();
        $table->column('created_at', __('创建时间'));

        if (HomeConfig::query()->count() >= 1) {
            $table->disableCreateButton();
        }

        return $table;
    }

    protected function form()
    {
        $form = new Form(new HomeConfig());

        $form->radio('type', __('类型'))->options(HomeConfig::$typeMap)->default(HomeConfig::TYPE_IMAGE)->rules('required');
        $form->file('mp4_or_image', '图片或者视频')->help('选择类型为图片，请上传jpg、png、gif文件格式;选择类型为视频，请上传mp4文件格式.')->rules('required');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
        });

        return $form;
    }
}
